package no.ntnu.fp.messages.impl;

import no.ntnu.fp.messages.Message;
import no.ntnu.fp.messages.MessageCode;
import no.ntnu.fp.model.Employee;
import no.ntnu.fp.model.Meeting;
import no.ntnu.fp.utils.XmlParser;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class DeleteMeetingMessage implements Message {
	private final Meeting meeting;
	private final Employee employee;
	
	public DeleteMeetingMessage(Meeting meeting, Employee employee) {
		this.meeting = meeting;
		this.employee = employee;
	}
	
	public DeleteMeetingMessage() {
		this(null, null);
	}

	public Meeting getMeeting() {
		return meeting;
	}

	public Employee getEmployee() {
		return employee;
	}

	@Override
	public MessageCode getMessageCode() {
		return MessageCode.DELETE_MEETING_MESSAGE;
	}

	@Override
	public Message fromXmlString(String xml) {
		Document doc = XmlParser.parseXmlString(xml);
		Meeting meeting = new Meeting((Element)doc.getElementsByTagName("meeting").item(0));
		Employee employee = new Employee((Element)doc.getElementsByTagName("employee").item(0));
		return new DeleteMeetingMessage(meeting, employee);
	}

	@Override
	public String toString() {
		Document doc = XmlParser.getThreadLocalDocumentBuilder().newDocument();
		Element messageElement = doc.createElement("message");
		messageElement.appendChild(meeting.toXml(doc));
		messageElement.appendChild(employee.toXml(doc));
		return getMessageCode() + XmlParser.convertXmlToString(messageElement);
	}

}
